﻿//-----------------------------------------------------------------------
// <copyright file="EveryoneOptions.cs" company="Sondre Bjellås">
// This software is licensed as Microsoft Public License (Ms-PL).
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Drawing;
using System.Windows.Forms;
using FlickrDownloadr.Services;

namespace FlickrDownloadr
{
    public partial class EveryoneOptions : UserControl, IWizardPage
    {
        public EveryoneOptions()
        {
            InitializeComponent();
        }

        public void ReloadPage()
        { }

        public Bitmap Icon
        {
            get { return Properties.Resources._1403_Globe; }
        }

        public event NextDelegate Next;

        public void OnNext(bool refresh)
        {
            if (Next != null)
            {
                Next(this, refresh);
            }
        }

        public SearchOptions SearchOptions { get; set; }

        public string Title
        {
            get { return Properties.Resources.PageEveryoneTitle; }
        }
        public string Description
        {
            get { return Properties.Resources.PageEveryoneDescription; }
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            string userInput = textBox1.Text.Trim();

            // This is used for the folder name
            PhotoService.Instance.SearchOptions.Text = userInput;

            if (radioButton1.Checked)
            {
                PhotoService.Instance.Options.Text = userInput;
                //PhotoService.Instance.SearchOptions.Type = SearchType.Search;
            }
            else
            {
                //PhotoService.Instance.SearchOptions.Type = SearchType.Tag;
                PhotoService.Instance.Options.Tags = userInput;
            }

            OnNext(true);

            //try
            //{
            //    string[] tags = PhotoService.Instance.SearchOptions.Text.Split(' ');
            //    var photos = FlickrFactory.GetInstance().PhotosSearch(tags);
                
            //    .PeopleFindByUsername(PhotoService.Instance.SearchOptions.Text);
            //    PhotoService.Instance.SearchOptions.UserId = user.UserId;
            //}
            //catch (System.Net.WebException wex)
            //{
            //    // Perhaps the firewall was turned on? Give the user a dialog to retry...

            //    DialogResult result = MessageBox.Show(Properties.Resources.ErrorNetworkAccess, Properties.Resources.ErrorNetworkAccessTitle, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);

            //    if (result == DialogResult.Retry)
            //    {
            //        ButtonNext_Click(sender, e);
            //    }
            //    else
            //    {
            //        return;
            //    }
            //}
            //catch (FlickrNet.FlickrApiException ex1)
            //{
            //    try
            //    {
            //        var user = FlickrFactory.GetInstance().PeopleFindByEmail(PhotoService.Instance.SearchOptions.Text);
            //        PhotoService.Instance.SearchOptions.UserId = user.UserId;
            //    }
            //    catch (FlickrNet.FlickrApiException ex2)
            //    {
            //        int code = ex2.Code;

            //        if (ex2.Code == 1)
            //        {
            //            MessageBox.Show("Unable to find the selected user. Please try another name.", "Wrong User", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //        }
            //        else
            //        {
            //            MessageBox.Show("Error occurred.\r\n\r\n" + ex2.ToString(), "Error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //        }

            //        return;
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("System error occurred.\r\n\r\n" + ex.ToString(), "System error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return;
            //}

        }

        private void EveryoneOptions_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                e.Handled = true;
                buttonNext.PerformClick();
            }
        }

    }
}
